<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul class="list">
        <li></li>
    </ul>
    <button>获取电影</button>
    <button>添加电影</button>
    <button>修改电影</button>
    <button>局部修改PATCH大写</button>
    <button>删除电影</button>
    <script>
        // 获取ul和按钮
        let ul = document.querySelector('ul');
        let btns = document.querySelectorAll('button');

        //获取电影列表
        btns[0].onclick = function () {
            fetch('http://api.zhibao.com/movies').then(val => val.json().then(val => val))
            .then(val => {
                val.forEach(element => {
                    let li = document.createElement('li');
                    li.innerHTML = element.name;
                    ul.appendChild(li);
                });
            });
        }

        // 添加电影
        btns[1].onclick = function(){
            fetch('http://api.zhibao.com/movies',{
                method:"POST",
                headers:{
                    // "content-type":"application/x-www-form-urlencoded"
                    "content-type":"application/json"
                },
                body:JSON.stringify({
                    name:"笑傲江湖",
                    director:"啊啊啊",
                    star:"令狐冲"
                })
                // body:"name=泰坦尼克号&director=大卫·xxx&star=小李子"
            }).then((val)=>{
                console.log(val);
            });

            // let x = new XMLHttpRequest();
            // x.open('post','http://api.zhibao.com/movies');
            // x.setRequestHeader('content-type','application/json');
            // x.send(JSON.stringify({
            //         name:"笑傲江湖22",
            //         director:"啊啊啊",
            //         star:"令狐冲"
            //     }));
            // x.onload=function(){
            //     console.log(x.response);
            // }

        }
    
        // 修改电影
        btns[2].onclick = function(){
            fetch('http://api.zhibao.com/movies/12',{
                method:"put",
                headers:{
                    "content-type":"application/json"
                },
                body:JSON.stringify({
                    name:"东邪西毒",
                    director:"吴彦祖",
                    star:"梁朝伟"
                })
            }).then(val=>{
                console.log(val);
            })
        }

        // 局部修改
        btns[3].onclick = function(){
            fetch('http://api.zhibao.com/movies/9',{
                method:"PATCH",
                headers:{
                    "content-type":"application/json"
                },
                body:JSON.stringify({
                    name:"东邪西毒999"
                })
            }).then(val=>{
                console.log(val);
            })
        }
    
        // 删除电影
        btns[4].onclick = function(){
            fetch('http://api.zhibao.com/movies/13',{
                method:"delete",
            }).then(val=>{
                console.log(val);
            })
        }
    </script>
</body>

</html>